package game.web.dao.impl;

import game.web.dao.ChargeDAO;
import game.web.dao.dto.ChargeDTO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class ChargeDAOImpl extends AbstractDAO implements ChargeDAO {

	public enum field {
		id, userid, money,orderstatus,orderno,cardno,createtime,updatetime,createip,updateip;
	}

	public ChargeDAOImpl() throws ProjectXSystemException {
		super();
	}
	
	public boolean insert(ChargeDTO dto) throws DataAccessException {
		int result = 0;
		PreparedStatement ps = null;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"ChargeDAO_INSERT");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, field.id.ordinal()+1, dto.getId(), Types.VARCHAR);
			_builder.setParameter(ps, field.userid.ordinal()+1, dto.getUserid(), Types.VARCHAR);
			_builder.setParameter(ps, field.money.ordinal()+1, dto.getMoney(), Types.DOUBLE);
			_builder.setParameter(ps, field.orderstatus.ordinal()+1, dto.getOrderstatus(), Types.INTEGER);
			_builder.setParameter(ps, field.orderno.ordinal()+1, dto.getOrderno(), Types.VARCHAR);
			_builder.setParameter(ps, field.cardno.ordinal()+1, dto.getCardno(), Types.VARCHAR);
			_builder.setParameter(ps, field.createtime.ordinal()+1, dto.getCreatetime(), Types.VARCHAR);
			_builder.setParameter(ps, field.updatetime.ordinal()+1, dto.getUpdatetime(), Types.VARCHAR);
			_builder.setParameter(ps, field.createip.ordinal()+1, dto.getCreateip(), Types.VARCHAR);
			_builder.setParameter(ps, field.updateip.ordinal()+1, dto.getUpdateip(), Types.VARCHAR);
			debugSql();
			result = ps.executeUpdate();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (result == 0) {
			_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
		} else if (result > 1) {
			_exHandler.handleUpdateException(null,
					ExceptionHandler.MULTIUPDATED);
		}
		return true;
	}

	@Override
	public List<ChargeDTO> listById(String userid) throws DataAccessException {
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = null;
		List<ChargeDTO> list = new ArrayList();
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"ChargeDAO_SELECT");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, 1, userid, Types.VARCHAR);
			debugSql();
			
			rs = ps.executeQuery();

			while (rs.next()) {
				String id = getString(rs.getString(field.id.ordinal()+1));
				String uid = getString(rs.getString(field.userid.ordinal()+1));
				double money = rs.getDouble(field.money.ordinal()+1);
				int orderstatus = rs.getInt(field.orderstatus.ordinal()+1);
				String orderno = getString(rs.getString(field.orderno.ordinal()+1));
				String cardno = getString(rs.getString(field.cardno.ordinal()+1));
				String createtime = rs.getString(field.createtime.ordinal()+1);
				String updatetime = rs.getString(field.updatetime.ordinal()+1);
				String createip = getString(rs.getString(field.createip.ordinal()+1));
				String updateip = getString(rs.getString(field.updateip.ordinal()+1));
				ChargeDTO dto = new ChargeDTO();
				dto.setId(id);
				dto.setUserid(uid);
				dto.setMoney(money);
				dto.setOrderstatus(orderstatus);
				dto.setOrderno(orderno);
				dto.setCardno(cardno);
				dto.setCreatetime(createtime);
				dto.setUpdatetime(updatetime);
				dto.setCreateip(createip);
				dto.setUpdateip(updateip);
				list.add(dto);
			}
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		return list;
	}

	@Override
	public List<ChargeDTO> listByDate(String userid, String startdate,
			String enddate) throws DataAccessException{
			ResultSet rs = null;
			PreparedStatement ps = null;
			String sql = null;
			List<ChargeDTO> list = new ArrayList();
			try {
				_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
						"ChargeDAO_SELECT_BY_Date");
				sql = _builder.getSql(null, null);
				ps = _builder.createStatement(_conn, sql, false, false);
				_builder.setParameter(ps, 1, userid, Types.VARCHAR);
				_builder.setParameter(ps, 2, startdate, Types.VARCHAR);
				_builder.setParameter(ps, 3, enddate, Types.VARCHAR);
				debugSql();
				
				rs = ps.executeQuery();
		
				while (rs.next()) {
					String id = getString(rs.getString(field.id.ordinal()+1));
					String uid = getString(rs.getString(field.userid.ordinal()+1));
					double money = rs.getDouble(field.money.ordinal()+1);
					int orderstatus = rs.getInt(field.orderstatus.ordinal()+1);
					String orderno = getString(rs.getString(field.orderno.ordinal()+1));
					String cardno = getString(rs.getString(field.cardno.ordinal()+1));
					String createtime = rs.getString(field.createtime.ordinal()+1);
					String updatetime = rs.getString(field.updatetime.ordinal()+1);
					String createip = getString(rs.getString(field.createip.ordinal()+1));
					String updateip = getString(rs.getString(field.updateip.ordinal()+1));
					ChargeDTO dto = new ChargeDTO();
					dto.setId(id);
					dto.setUserid(uid);
					dto.setMoney(money);
					dto.setOrderstatus(orderstatus);
					dto.setOrderno(orderno);
					dto.setCardno(cardno);
					dto.setCreatetime(createtime);
					dto.setUpdatetime(updatetime);
					dto.setCreateip(createip);
					dto.setUpdateip(updateip);
					list.add(dto);
				}
			} catch (Exception e) {
				errorSql(e);
				_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
			} finally {
				try {
					if (rs != null) {
						rs.close();
					}
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					logError(e);
				}
			}
			
			System.err.println("Bydate select size=="+list.size());
			return list;
		}

}
